home *** CD-ROM | disk | FTP | other *** search
- /* NewReplaceTool.c
- *
- * MPW tool to provide an enhanced front end to the standard MPW shell replace command
- */
-
- #include <OSUtils.h>
- #include <Strings.h>
- #include <Signal.h>
- #include <StdLib.h>
- #include <Scrap.h>
- #include <ToolUtils.h>
- #include <Lists.h>
- #include <Types.h>
- #include <Dialogs.h>
- #include <TextEdit.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Fonts.h>
- #include <Quickdraw.h>
- #include <StdIo.h>
- #include <String.h>
-
- pascal char InputFilter(DialogPtr pdlogReplace,EventRecord *pevntReplace,short *psHit);
- void RefreshDialog(DialogPtr pdlogReplace);
- void QuoteString(char *pszOutString,char *pszInString);
- void ReplaceDialog(void);
- void main(void);
-
- #define REPLACE_BUTTON 1
- #define CANCEL_BUTTON 2
- #define REPLACE_ALL_BUTTON 3
- #define FIND_BUTTON 4
- #define CASE_SENSITIVE_CHECK 5
- #define SEARCH_BACKWARD_CHECK 6
- #define LITERAL_RADIO 7
- #define ENTIRE_WORD_RADIO 8
- #define SELECTION_EXPRESSION_RADIO 9
- #define FIND_TEXT_EDIT 10
- #define REPLACE_TEXT_EDIT 11
-
- char chDialogExit;
- char chDoubleClick;
- Point ptAction;
- ListHandle hlistFind;
- Rect rectFindList;
- ListHandle hlistReplace;
- Rect rectReplaceList;
- short sFontNum;
-
- pascal char InputFilter(DialogPtr pdlogReplace,EventRecord *pevntReplace,short *psHit)
- /* dialog input filter */
-
- {
- Rect rectTemp;
- char chRetVal,chTest;
- short sDialogType,sLen;
- Handle hDialogItem;
- int iKeyCode,iCharCode;
- Boolean fCmdDown;
- Cell cellTemp;
- Str255 strTemp;
-
- chRetVal = false; /* initialize */
- if (pevntReplace->what == mouseDown)
- {
- /* there was a mouse down event */
- ptAction = pevntReplace->where; /* get click position */
- GlobalToLocal(&ptAction); /* convert to local coordinates */
- if (PtInRect(ptAction,&rectFindList) == true)
- {
- /* an event occured in the find text list */
- chDoubleClick = LClick(ptAction,pevntReplace->modifiers,hlistFind);
- if (chDoubleClick == true)
- {
- cellTemp = LLastClick(hlistFind); /* find where the click occured */
- sLen = 255; /* maximum data length */
- LGetCell((strTemp + 1),&sLen,cellTemp,hlistFind); /* get a copy of the clicked on text */
- strTemp[0] = (unsigned char) sLen; /* set the length byte */
- GetDItem(pdlogReplace,FIND_TEXT_EDIT,&sDialogType,&hDialogItem,&rectTemp); /* get the item handle */
- SetIText(hDialogItem,strTemp); /* set the new text string */
- SelIText(pdlogReplace,FIND_TEXT_EDIT,sLen,sLen); /* set the new insertion point */
- *psHit = FIND_TEXT_EDIT; /* signal item affected */
- chRetVal = true; /* signal processing complete */
- } /* if chDoubleClick */
- } /* if PtInRect() */
- if (PtInRect(ptAction,&rectReplaceList) == true)
- {
- /* an event occured in the replace text list */
- chDoubleClick = LClick(ptAction,pevntReplace->modifiers,hlistReplace);
- if (chDoubleClick == true)
- {
- cellTemp = LLastClick(hlistReplace); /* find where the click occured */
- sLen = 255; /* maximum data length */
- LGetCell((strTemp + 1),&sLen,cellTemp,hlistReplace); /* get a copy of the clicked on text */
- strTemp[0] = (unsigned char) sLen; /* set the length byte */
- GetDItem(pdlogReplace,REPLACE_TEXT_EDIT,&sDialogType,&hDialogItem,&rectTemp); /* get the item handle */
- SetIText(hDialogItem,strTemp); /* set the new text string */
- SelIText(pdlogReplace,REPLACE_TEXT_EDIT,sLen,sLen); /* set the new insertion point */
- *psHit = REPLACE_TEXT_EDIT; /* signal item affected */
- chRetVal = true; /* signal processing complete */
- } /* if chDoubleClick */
- } /* if PtInRect() */
- } /* if pevntReplace */
- else
- {
- if (pevntReplace->what == keyDown)
- {
- /* there was a keypress event */
- iKeyCode = pevntReplace->message & keyCodeMask; /* get the key code */
- iKeyCode = iKeyCode >> 8; /* shift to low order byte */
- if ((iKeyCode == 0x24) || (iKeyCode == 0x4c))
- {
- /* Return or Enter were pressed - treat as exit request */
- chRetVal = true; /* signal processing complete */
- *psHit = ok; /* signal item affected */
- } /* if iKeyCode */
- else
- {
- fCmdDown = ((pevntReplace->modifiers & cmdKey) != 0); /* is the Command key down? */
- if (fCmdDown)
- {
- *psHit = ((DialogPeek) pdlogReplace)->editField + 1; /* signal item effected (current TE field) */
- iCharCode = pevntReplace->message & charCodeMask; /* get the character code */
- chTest = (char) iCharCode; /* convert it to a character */
- switch (chTest)
- {
- case 'x' :
- {
- DlgCut(pdlogReplace); /* perform cut to scrap */
- chRetVal = true; /* signal processing complete */
- break;
- } /* case 'x' */
- case 'c' :
- {
- DlgCopy(pdlogReplace); /* perform copy to scrap */
- chRetVal = true; /* signal processing complete */
- break;
- } /* case 'c' */
- case 'v' :
- {
- DlgPaste(pdlogReplace); /* perform paste from scrap */
- chRetVal = true; /* signal processing complete */
- break;
- } /* case 'v' */
- case '.' :
- {
- chRetVal = true; /* signal processing complete */
- *psHit = cancel; /* signal item affected */
- break;
- } /* case '.' */
- } /* switch chTest */
- } /* if fCmdDown */
- } /* else if iKeyCode */
- } /* if pevntReplace */
- else
- {
- if (pevntReplace->what == updateEvt)
- {
- if (IsDialogEvent(pevntReplace))
- {
- RefreshDialog(pdlogReplace); /* update the non-controls */
- } /* if IsDialogEvent() */
- } /* if pevntReplace */
- } /* else if pevntReplace */
- } /* else if pevntReplace */
- return chRetVal; /* signal if processing is complete */
- } /* InputFilter() */
-
- void RefreshDialog(DialogPtr pdlogReplace)
- /* update non-controls in the dialog */
-
- {
- Rect rectTemp;
- short sDialogType,sTemp;
- Handle hDialogItem;
- ControlHandle hctrlItem;
-
- GetDItem(pdlogReplace,REPLACE_BUTTON,&sDialogType,&hDialogItem,&rectTemp); /* get the item handle */
- PenSize(3,3); /* change pen to draw thick default outline */
- InsetRect(&rectTemp,-4,-4); /* draw outside the button by 1 pixel */
- FrameRoundRect(&rectTemp,16,16); /* draw the outline */
- PenSize(1,1); /* restore the pen size to the default value */
-
- /* Draw the upper divide line */
- PenPat(qd.gray); /* set this to a gray line */
- MoveTo(15,204); /* move to starting position */
- LineTo(585,204); /* draw to ending position */
-
- /* Draw the lower divide line */
- MoveTo(15,280); /* move to starting position */
- LineTo(585,280); /* draw to ending position */
- PenPat(qd.black); /* revert to default pen pattern */
-
- /* Draw the find prompt text */
- TextFont(systemFont);
- TextSize(12);
- GetDItem(pdlogReplace,SELECTION_EXPRESSION_RADIO,&sDialogType,&hDialogItem,&rectTemp); /* get the item handle */
- hctrlItem = (ControlHandle) hDialogItem; /* change dialog handle to control handle */
- sTemp = GetCtlValue(hctrlItem); /* get the current Checkbox value */
- SetRect(&rectTemp,15,153,290,169);
- EraseRect(&rectTemp);
- MoveTo(15,166);
- if (sTemp)
- {
- DrawString("\pFind what selection expression?");
- } /* if sTemp */
- else
- {
- DrawString("\pFind what string?");
- } /* else if sTemp */
-
- /* Draw the replace prompt text */
- SetRect(&rectTemp,310,153,585,169);
- EraseRect(&rectTemp);
- MoveTo(310,166);
- DrawString("\pReplace with what string?");
- TextFont(sFontNum);
- TextSize(9);
-
- LUpdate(pdlogReplace->visRgn,hlistFind); /* update this list */
- rectTemp = rectFindList; /* start with full size */
- InsetRect(&rectTemp,-1,-1); /* set for framing */
- FrameRect(&rectTemp); /* frame it */
-
- LUpdate(pdlogReplace->visRgn,hlistReplace); /* update this list */
- rectTemp = rectReplaceList; /* start with full size */
- InsetRect(&rectTemp,-1,-1); /* set for framing */
- FrameRect(&rectTemp); /* frame it */
- } /* RefreshDialog() */
-
- void AddListString(Str255 *strAdd,ListHandle hlstString)
- /* add strings to an existing list */
-
- {
- short sRow;
- Point ptSize;
-
- if (hlstString != nil)
- {
- ptSize.h = 0; /* point to the correct column */
- sRow = LAddRow(1,32000,hlstString); /* add another row at the end of the list */
- ptSize.v = sRow; /* point to the row just added */
- LSetCell((*strAdd + 1),*strAdd[0],ptSize,hlstString); /* place string in row just created */
- LDraw(ptSize,hlstString); /* draw the new string */
- } /* if hlstString */
- } /* AddListString() */
-
- void QuoteString(char *pszOutString,char *pszInString)
- /* quote ' characters in the input string */
-
- {
- char *pchEnd,*pchLast,*pchStart;
-
- *pszOutString = '\0'; /* initialize to empty */
- pchStart = pszInString; /* initialize */
- pchEnd = pszInString; /* initialize */
- pchLast = pchStart + strlen(pszInString);
- while (pchEnd != NULL)
- {
- pchEnd = strchr(pchStart,(int) '\''); /* find the next occurance of the character ', if any */
- if ((pchEnd != NULL) && (pchStart <= pchLast))
- {
- /* a ' character was found */
- (void) strncat(pszOutString,pchStart,(size_t) (pchEnd - pchStart)); /* concatenate the input string up to but not including the ' character */
- (void) strcat(pszOutString,"'∂''"); /* quote the ' character as ∂' */
- pchStart = pchEnd + 1; /* update the string start position */
- } /* if pchEnd */
- else
- {
- /* no more ' characters to be found */
- (void) strcat(pszOutString,pchStart); /* concatenate the remaining input string */
- } /* else if pchEnd */
- } /* while pchEnd */
- } /* QuoteString() */
-
- void ReplaceDialog(void)
- /* Find and Replace dialog box routine */
-
- {
- DialogPtr pdlogReplace;
- Rect rectData,rectTemp;
- short sDialogType,sIndex,sHit,sSearchType,sTemp;
- Boolean fBeep,fGoOn,fMatch,fReplace,fReplaceAll,fReverse,fSearchBackward;
- Handle hdlogItem;
- ControlHandle hctrlItem,hcrtlTemp;
- Str255 strTemp1,strTemp2;
- Point ptCSize;
- TEHandle hteString;
- DialogPeek pkdlogReplace;
- char *pszEnv;
- char achTemp[256],achQuote1[256],achQuote2[256];
- size_t usLen;
- FILE *pfilFindCommand,*pfilReplaceCommand,*pfilSetup;
- FontInfo fntiPrompt;
- KeyMap kmapExit;
-
- pdlogReplace = GetNewDialog(2,nil,(WindowPtr) -1); /* bring in the dialog resource */
- pszEnv = getenv("font");
- (void) strcpy(achTemp,pszEnv);
- c2pstr(achTemp);
- GetFNum(achTemp,&sFontNum);
-
- rectTemp.top = pdlogReplace->portRect.top; /* get window size, we will now center it */
- rectTemp.left = pdlogReplace->portRect.left; /* ditto */
- rectTemp.bottom = pdlogReplace->portRect.bottom; /* ditto */
- rectTemp.right = pdlogReplace->portRect.right; /* ditto */
- rectTemp.top = -pdlogReplace->portBits.bounds.top; /* fixed vertical position */
- rectTemp.left = ((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - (rectTemp.right - rectTemp.left)) / 2; /* center horizontally */
- MoveWindow(pdlogReplace,rectTemp.left,rectTemp.top,true); /* move the window to the proper position */
- SetPort(pdlogReplace); /* prepare to add conditional text */
-
- TextFont(sFontNum);
- TextSize(9);
- GetFontInfo(&fntiPrompt);
- pkdlogReplace = (DialogPeek) pdlogReplace; /* get to the inner record */
- hteString = pkdlogReplace->textH; /* get to the TE record */
- HLock((Handle) hteString); /* lock it for safety */
- (*hteString)->txFont = sFontNum;
- (*hteString)->txSize = 9;
- (*hteString)->fontAscent = fntiPrompt.ascent;
- (*hteString)->lineHeight = fntiPrompt.ascent + fntiPrompt.descent + fntiPrompt.leading;
- HUnlock((Handle) hteString); /* unLock the handle when done */
-
- SetRect(&rectFindList,15,15,290,147);
- rectTemp = rectFindList; /* start with full size */
- rectTemp.right = rectTemp.right - 15; /* make room for the scroll bar on the right */
- if (rectTemp.right <= (rectTemp.left + 15))
- {
- /* Safety check */
- rectTemp.right = rectTemp.left + 15;
- } /* if rectTemp */
- InsetRect(&rectTemp,-1,-1); /* set for framing */
- FrameRect(&rectTemp); /* frame it */
- InsetRect(&rectTemp,1,1); /* restore */
- SetRect(&rectData,0,0,1,0); /* set up list */
- ptCSize.h = rectTemp.right - rectTemp.left; /* width of the list */
- ptCSize.v = 0; /* let List Manager determine the height */
- hlistFind = LNew(&rectTemp,&rectData,ptCSize,0,pdlogReplace,true,false,false,true); /* make the list */
- HLock((Handle) hlistFind);
- (*hlistFind)->selFlags = lOnlyOne + lNoNilHilite; /* set the attributes */
- HUnlock((Handle) hlistFind);
- LDoDraw(true,hlistFind); /* draw the list */
-
- pszEnv = getenv("findstrings"); /* get a pointer to the FindStrings environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"r"); /* try to open the file for reading */
- if (pfilSetup != NULL)
- {
- sIndex = 0;
- while (!feof(pfilSetup))
- {
- if (fgets(achTemp,256,pfilSetup) != NULL)
- {
- usLen = strlen(achTemp);
- if (usLen > 0)
- {
- if (*(achTemp + usLen - 1) == (char) '\n')
- {
- usLen--;
- *(achTemp + usLen) = '\0'; /* remove the line feed character */
- } /* if *achTemp */
- c2pstr(achTemp);
- AddListString((Str255 *)achTemp,hlistFind);
- sIndex++;
- } /* if usLen */
- } /* if fgets() */
- } /* while !feof() */
- (void) fclose(pfilSetup);
- if (sIndex > 0)
- {
- ptCSize.v = sIndex - 1; /* cell row of last item */
- ptCSize.h = 0; /* set cell column */
- LSetSelect(true,ptCSize,hlistFind); /* select the cell */
- LAutoScroll(hlistFind); /* scroll as necessary to display the selected item */
- } /* if sIndex */
- } /* if pfilSetup */
- } /* if pszEnv */
-
- SetRect(&rectReplaceList,310,15,585,147);
- rectTemp = rectReplaceList; /* start with full size */
- rectTemp.right = rectTemp.right - 15; /* make room for the scroll bar on the right */
- if (rectTemp.right <= (rectTemp.left + 15))
- {
- /* safety check */
- rectTemp.right = rectTemp.left + 15;
- } /* if rectTemp */
- InsetRect(&rectTemp,-1,-1); /* set for framing */
- FrameRect(&rectTemp); /* frame it */
- InsetRect(&rectTemp,1,1); /* restore */
- SetRect(&rectData,0,0,1,0); /* set list up */
- ptCSize.h = rectTemp.right - rectTemp.left; /* width of the list */
- ptCSize.v = 0; /* let List Manager determine the height */
- hlistReplace = LNew(&rectTemp,&rectData,ptCSize,0,pdlogReplace,true,false,false,true); /* make the list */
- HLock((Handle) hlistReplace);
- (*hlistReplace)->selFlags = lOnlyOne + lNoNilHilite; /* set the attributes */
- HUnlock((Handle) hlistReplace);
- LDoDraw(true,hlistReplace); /* draw the list */
-
- pszEnv = getenv("replacestrings"); /* get a pointer to the FindStrings environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"r"); /* try to open the file for reading */
- if (pfilSetup != NULL)
- {
- sIndex = 0;
- while (!feof(pfilSetup))
- {
- if (fgets(achTemp,256,pfilSetup) != NULL)
- {
- usLen = strlen(achTemp);
- if (usLen > 0)
- {
- if (*(achTemp + usLen - 1) == (char) '\n')
- {
- usLen--;
- *(achTemp + usLen) = '\0'; /* remove the line feed character */
- } /* if *achTemp */
- c2pstr(achTemp);
- AddListString((Str255 *)achTemp,hlistReplace);
- sIndex++;
- } /* if usLen */
- } /* if fgets() */
- } /* while !feof() */
- (void) fclose(pfilSetup);
- if (sIndex > 0)
- {
- ptCSize.v = sIndex - 1; /* cell row of last item */
- ptCSize.h = 0; /* set cell column */
- LSetSelect(true,ptCSize,hlistReplace); /* select the cell */
- LAutoScroll(hlistReplace); /* scroll as necessary to display the selected item */
- } /* if sIndex */
- } /* if pfilSetup */
- } /* if pszEnv */
-
- /* set up check boxes according to the enviroment variables */
- GetDItem(pdlogReplace,CASE_SENSITIVE_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- pszEnv = getenv("casesensitive"); /* get a pointer to the CaseSensitive environment variable */
- if (atoi(pszEnv) == 0)
- {
- SetCtlValue(hcrtlTemp,0); /* turn the check box off */
- } /* if atoi() */
- else
- {
- SetCtlValue(hcrtlTemp,1); /* turn the check box on */
- } /* else if atoi() */
- GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- pszEnv = getenv("searchbackward"); /* get a pointer to the SearchBackward environment variable */
- if (atoi(pszEnv) == 0)
- {
- SetCtlValue(hcrtlTemp,0); /* turn the check box off */
- } /* if atoi() */
- else
- {
- SetCtlValue(hcrtlTemp,1); /* turn the check box on */
- } /* else if atoi() */
-
- /* set up radio buttons according to the enviroment variable */
- for (sIndex = LITERAL_RADIO; sIndex <= SELECTION_EXPRESSION_RADIO; sIndex++)
- {
- /* clear all radios */
- GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp); /* get the Radio handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* get the control handle */
- SetCtlValue(hcrtlTemp,0); /* turn the radio selection off */
- } /* for sIndex */
- pszEnv = getenv("searchtype"); /* get a pointer to the SearchType environment variable */
- sIndex = LITERAL_RADIO + atoi(pszEnv); /* get the default radio sIndex */
- GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hctrlItem = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- SetCtlValue(hctrlItem,1); /* select the radio */
-
- /* set up initial highlighting */
- if (sIndex == SELECTION_EXPRESSION_RADIO)
- {
- GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- HiliteControl(hcrtlTemp,255); /* set to disabled */
- } /* if sIndex */
-
- /* set up the default replacement string according to the enviroment variables */
- GetDItem(pdlogReplace,REPLACE_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- pszEnv = getenv("lastreplacestring"); /* get a pointer to the LastReplaceString environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"r"); /* try to open the file for reading */
- if (pfilSetup != NULL)
- {
- if (fgets(achTemp,256,pfilSetup) != NULL)
- {
- usLen = strlen(achTemp);
- if (usLen > 0)
- {
- if (*(achTemp + usLen - 1) == (char) '\n')
- {
- usLen--;
- *(achTemp + usLen) = '\0'; /* remove the line feed character */
- } /* if *achTemp */
- } /* if usLen */
- } /* if fgets() */
- (void) fclose(pfilSetup);
- } /* if pfilSetup */
- c2pstr(achTemp);
- SetIText(hdlogItem,achTemp);
- SelIText(pdlogReplace,REPLACE_TEXT_EDIT,0,(short) strlen(pszEnv)); /* set the new insertion point */
- } /* if pszEnv */
- else
- {
- SetIText(hdlogItem,"\p");
- SelIText(pdlogReplace,REPLACE_TEXT_EDIT,0,0); /* set the new insertion point */
- } /* else if pszEnv */
-
- /* set up the default find string/selection expression according to the enviroment variables */
- GetDItem(pdlogReplace,FIND_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- pszEnv = getenv("lastfindstring"); /* get a pointer to the LastFindString environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"r"); /* try to open the file for reading */
- if (pfilSetup != NULL)
- {
- if (fgets(achTemp,256,pfilSetup) != NULL)
- {
- usLen = strlen(achTemp);
- if (usLen > 0)
- {
- if (*(achTemp + usLen - 1) == (char) '\n')
- {
- usLen--;
- *(achTemp + usLen) = '\0'; /* remove the line feed character */
- } /* if *achTemp */
- } /* if usLen */
- } /* if fgets() */
- (void) fclose(pfilSetup);
- } /* if pfilSetup */
- c2pstr(achTemp);
- SetIText(hdlogItem,achTemp);
- SelIText(pdlogReplace,FIND_TEXT_EDIT,0,(short) strlen(pszEnv)); /* set the new insertion point */
- } /* if pszEnv */
- else
- {
- SetIText(hdlogItem,"\p");
- SelIText(pdlogReplace,FIND_TEXT_EDIT,0,0); /* set the new insertion point */
- } /* else if pszEnv */
-
- RefreshDialog(pdlogReplace); /* draw the non-controls */
- ShowWindow(pdlogReplace); /* open a dialog box */
- SelectWindow(pdlogReplace); /* make it visible */
-
- chDialogExit = false; /* do not exit dialog handle loop yet */
- do
- {
- ModalDialog((ModalFilterProcPtr) InputFilter,&sHit); /* wait until an item is hit */
- GetDItem(pdlogReplace,sHit,&sDialogType,&hdlogItem,&rectTemp); /* get item information */
- hctrlItem = (ControlHandle) hdlogItem; /* get the control handle */
-
- if (sHit == REPLACE_BUTTON)
- {
- chDialogExit = true; /* exit the dialog when this selection is made */
- } /* if sHit */
-
- if (sHit == CANCEL_BUTTON)
- {
- chDialogExit = true; /* exit the dialog when this selection is made */
- } /* if sHit */
-
- if (sHit == REPLACE_ALL_BUTTON)
- {
- chDialogExit = true; /* exit the dialog when this selection is made */
- } /* if sHit */
-
- if (sHit == FIND_BUTTON)
- {
- chDialogExit = true; /* exit the dialog when this selection is made */
- } /* if sHit */
-
- if (sHit == CASE_SENSITIVE_CHECK)
- {
- sTemp = GetCtlValue(hctrlItem); /* get the current Checkbox value */
- SetCtlValue(hctrlItem,((sTemp + 1) & 1)); /* toggle the value */
- } /* if sHit */
-
- if (sHit == SEARCH_BACKWARD_CHECK)
- {
- sTemp = GetCtlValue(hctrlItem); /* get the current Checkbox value */
- SetCtlValue(hctrlItem,((sTemp + 1) & 1)); /* toggle the value */
- } /* if sHit */
-
- if ((sHit >= LITERAL_RADIO) && (sHit <= SELECTION_EXPRESSION_RADIO))
- {
- if (sHit == SELECTION_EXPRESSION_RADIO)
- {
- sTemp = GetCtlValue(hctrlItem); /* get the current Checkbox value */
- if (!(sTemp & 1))
- {
- /* disable SEARCH_BACKWARD_CHECK */
- GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- HiliteControl(hcrtlTemp,255); /* set to disabled */
-
- /* update the find prompt text */
- SetRect(&rectTemp,15,153,290,169);
- EraseRect(&rectTemp);
- TextFont(systemFont);
- TextSize(12);
- MoveTo(15,166);
- DrawString("\pFind what selection expression?");
- TextFont(sFontNum);
- TextSize(9);
- } /* else if !sTemp */
- } /* if sHit */
- else
- {
- GetDItem(pdlogReplace,SELECTION_EXPRESSION_RADIO,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- sTemp = GetCtlValue(hcrtlTemp); /* get the current Checkbox value */
- if (sTemp & 1)
- {
- /* enable SEARCH_BACKWARD_CHECK */
- GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* change dialog handle to control handle */
- HiliteControl(hcrtlTemp,0); /* set to no highlighting */
-
- /* update the find prompt text */
- SetRect(&rectTemp,15,153,290,169);
- EraseRect(&rectTemp);
- TextFont(systemFont);
- TextSize(12);
- MoveTo(15,166);
- DrawString("\pFind what string?");
- TextFont(sFontNum);
- TextSize(9);
- } /* if sTemp */
- } /* else if sHit */
- for (sIndex = LITERAL_RADIO; sIndex <= SELECTION_EXPRESSION_RADIO; sIndex++)
- {
- /* clear all radios */
- GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp); /* get the Radio handle */
- hcrtlTemp = (ControlHandle) hdlogItem; /* get the control handle */
- SetCtlValue(hcrtlTemp,0); /* turn the radio selection off */
- } /* for sIndex */
- SetCtlValue(hctrlItem,1); /* turn the one radio selection on */
- } /* if sHit */
- } /* do */
- while (chDialogExit == false);
-
- /* Get results after dialog */
- fGoOn = true; /* initialize */
- fReplace = true; /* initialize */
- fReplaceAll = false; /* initialize */
- GetKeys(kmapExit);
- if ((kmapExit[1] & 1) > 0)
- {
- /* a shift key was held down when exiting the dialog - sTemporarily reverse the search direction */
- fReverse = true;
- } /* if kmapExit[] */
- else
- {
- fReverse = false;
- } /* else if kmapExit[] */
- switch (sHit)
- {
- case REPLACE_BUTTON :
- {
- /* do nothing */
- break;
- } /* case REPLACE_BUTTON */
- case CANCEL_BUTTON :
- {
- fGoOn = false;
- break;
- } /* case CANCEL_BUTTON */
- case REPLACE_ALL_BUTTON :
- {
- fReplaceAll = true;
- break;
- } /* case REPLACE_ALL_BUTTON */
- case FIND_BUTTON :
- {
- fReplace = false;
- break;
- } /* case FIND_BUTTON */
- } /* switch sHit */
-
- if (fGoOn)
- {
- fBeep = true; /* initialize */
- GetDItem(pdlogReplace,FIND_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- GetIText(hdlogItem,&strTemp1); /* get the text entered */
- if ((int) strTemp1[0] > 0)
- {
- pfilReplaceCommand = fopen(getenv("replaceagainscript"),"w");
- if (pfilReplaceCommand != NULL)
- {
- pfilFindCommand = fopen(getenv("findagainscript"),"w");
- if (pfilFindCommand != NULL)
- {
- /* the command output files were successfully opened */
- fBeep = false; /* disable error beep */
- GetDItem(pdlogReplace,CASE_SENSITIVE_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the Checkbox handle */
- hctrlItem = (ControlHandle) hdlogItem; /* get the Checkbox handle */
- (void) fprintf(pfilReplaceCommand,"Set CaseSensitive %d\n",GetCtlValue(hctrlItem));
-
- GetDItem(pdlogReplace,SEARCH_BACKWARD_CHECK,&sDialogType,&hdlogItem,&rectTemp); /* get the Checkbox handle */
- hctrlItem = (ControlHandle) hdlogItem; /* get the Checkbox handle */
- fSearchBackward = (Boolean) GetCtlValue(hctrlItem);
- (void) fprintf(pfilReplaceCommand,"Set SearchBackward %u\n",fSearchBackward);
-
- sIndex = LITERAL_RADIO; /* start at the first radio in this group */
- do
- {
- GetDItem(pdlogReplace,sIndex,&sDialogType,&hdlogItem,&rectTemp); /* get the radio handle */
- hctrlItem = (ControlHandle) hdlogItem; /* get the radio handle */
- sTemp = GetCtlValue(hctrlItem); /* get the radio value */
- sIndex++;
- } /* do */
- while ((sTemp != 1) && (sIndex <= SELECTION_EXPRESSION_RADIO));
- sSearchType = sIndex - LITERAL_RADIO - 1;
- (void) fprintf(pfilReplaceCommand,"Set SearchType %d\n",sSearchType);
-
- p2cstr(strTemp1);
- pszEnv = getenv("lastfindstring"); /* get a pointer to the LastFindString environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"w"); /* try to open the file for writing */
- if (pfilSetup != NULL)
- {
- (void) fprintf(pfilSetup,"%s\n",strTemp1);
- (void) fclose(pfilSetup);
- } /* if pfilSetup */
- } /* if pszEnv */
-
- /* add the new find string to the saved find string file, but only if it is unique */
- pszEnv = getenv("findstrings"); /* get a pointer to the FindStrings environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"r+"); /* try to open the file for reading */
- if (pfilSetup != NULL)
- {
- fMatch = false; /* initialize */
- while (!feof(pfilSetup) && !fMatch)
- {
- if (fgets(achTemp,256,pfilSetup) != NULL)
- {
- usLen = strlen(achTemp);
- if (usLen > 0)
- {
- if (*(achTemp + usLen - 1) == (char) '\n')
- {
- usLen--;
- *(achTemp + usLen) = '\0'; /* remove the line feed character */
- } /* if *achTemp */
- fMatch = fMatch || (strcmp(achTemp,strTemp1) == 0);
- } /* if usLen */
- } /* if fgets() */
- } /* while !feof() */
- if (!fMatch)
- {
- (void) fprintf(pfilSetup,"%s\n",strTemp1); /* add the unique new string to the end of the file */
- } /* if !fMatch */
- (void) fclose(pfilSetup);
- } /* if pfilSetup */
- } /* if pszEnv */
-
- if (fReverse)
- {
- fSearchBackward = !fSearchBackward;
- } /* if fReverse */
-
- if (fReplace)
- {
- GetDItem(pdlogReplace,REPLACE_TEXT_EDIT,&sDialogType,&hdlogItem,&rectTemp); /* get the item handle */
- GetIText(hdlogItem,&strTemp2); /* get the text entered */
- p2cstr(strTemp2);
- pszEnv = getenv("lastreplacestring"); /* get a pointer to the LastFindString environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"w"); /* try to open the file for writing */
- if (pfilSetup != NULL)
- {
- (void) fprintf(pfilSetup,"%s\n",strTemp2);
- (void) fclose(pfilSetup);
- } /* if pfilSetup */
- } /* if pszEnv */
-
-
- /* add the new replace string to the saved replace string file, but only if it is unique */
- pszEnv = getenv("replacestrings"); /* get a pointer to the FindStrings environment variable */
- if (pszEnv != NULL)
- {
- pfilSetup = fopen(pszEnv,"r+"); /* try to open the file for reading */
- if (pfilSetup != NULL)
- {
- fMatch = false; /* initialize */
- while (!feof(pfilSetup) && !fMatch)
- {
- if (fgets(achTemp,256,pfilSetup) != NULL)
- {
- usLen = strlen(achTemp);
- if (usLen > 0)
- {
- if (*(achTemp + usLen - 1) == (char) '\n')
- {
- usLen--;
- *(achTemp + usLen) = '\0'; /* remove the line feed character */
- } /* if *achTemp */
- fMatch = fMatch || (strcmp(achTemp,strTemp2) == 0);
- } /* if usLen */
- } /* if fgets() */
- } /* while !feof() */
- if (!fMatch)
- {
- (void) fprintf(pfilSetup,"%s\n",strTemp2); /* add the unique new string to the end of the file */
- } /* if !fMatch */
- (void) fclose(pfilSetup);
- } /* if pfilSetup */
- } /* if pszEnv */
-
- switch (sSearchType)
- {
- case 0 : /* literal */
- {
- QuoteString(achQuote1,strTemp1); /* quote ' characters as necessary */
- QuoteString(achQuote2,strTemp2); /* quote ' characters as necessary */
- if (fReplaceAll)
- {
- if (fSearchBackward)
- {
- (void) fprintf(pfilReplaceCommand,"Replace -c ∞ \\'%s'\\ '%s' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
- } /* if fSearchBackward */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Replace -c ∞ /'%s'/ '%s' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
- } /* else if fSearchBackward */
- } /* if fReplaceAll */
- else
- {
- if (fSearchBackward)
- {
- (void) fprintf(pfilReplaceCommand,"Replace \\'%s'\\ '%s' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
- } /* if fSearchBackward */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Replace /'%s'/ '%s' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
- } /* else if fSearchBackward */
- } /* else if fReplaceAll */
- (void) fprintf(pfilReplaceCommand,"if {Status} != 0\nBeep\nend\n");
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
- break;
- } /* case 0 */
- case 1 : /* entire word */
- {
- QuoteString(achQuote1,strTemp1); /* quote ' characters as necessary */
- QuoteString(achQuote2,strTemp2); /* quote ' characters as necessary */
- if (fReplaceAll)
- {
- if (fSearchBackward)
- {
- (void) fprintf(pfilReplaceCommand,"Replace -c ∞ \\([¬{WordSet}])®1'%s'([¬{WordSet}])®2\\ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
- (void) fprintf(pfilFindCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
- (void) fprintf(pfilFindCommand,"Unmark '_New_Find_' \"{Active}\"\n");
- } /* if fSearchBackward */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Replace -c ∞ /([¬{WordSet}])®1'%s'([¬{WordSet}])®2/ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
- } /* else if fSearchBackward */
- } /* if fReplaceAll */
- else
- {
- if (fSearchBackward)
- {
- (void) fprintf(pfilReplaceCommand,"Replace \\([¬{WordSet}])®1'%s'([¬{WordSet}])®2\\ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
- (void) fprintf(pfilFindCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
- (void) fprintf(pfilFindCommand,"Unmark '_New_Find_' \"{Active}\"\n");
- } /* if fSearchBackward */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Replace /([¬{WordSet}])®1'%s'([¬{WordSet}])®2/ '®1''%s''®2' \"{Active}\"\n",achQuote1,achQuote2);
- (void) fprintf(pfilFindCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
- } /* else if fSearchBackward */
- } /* else if fReplaceAll */
- (void) fprintf(pfilReplaceCommand,"if {Status} != 0\nBeep\nend\n");
- break;
- } /* case 1 */
- case 2 : /* selection expression */
- {
- if (fReplaceAll)
- {
- (void) fprintf(pfilReplaceCommand,"Replace -c ∞ %s '%s' \"{Active}\"\n",strTemp1,strTemp2);
- } /* if fReplaceAll */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Replace %s '%s' \"{Active}\"\n",strTemp1,strTemp2);
- } /* else if fReplaceAll */
- (void) fprintf(pfilReplaceCommand,"if {Status} != 0\nBeep\nend\n");
- (void) fprintf(pfilFindCommand,"Find %s \"{Active}\"\n",strTemp1);
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
- break;
- } /* case 2 */
- } /* switch sSearchType */
- } /* if fReplace */
- else
- {
- switch (sSearchType)
- {
- case 0 : /* literal */
- {
- QuoteString(achQuote1,strTemp1); /* quote ' characters as necessary */
- if (fSearchBackward)
- {
- (void) fprintf(pfilReplaceCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
- (void) fprintf(pfilFindCommand,"Find \\'%s'\\ \"{Active}\"\n",achQuote1);
- } /* if fSearchBackward */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
- (void) fprintf(pfilFindCommand,"Find /'%s'/ \"{Active}\"\n",achQuote1);
- } /* else if fSearchBackward */
- (void) fprintf(pfilReplaceCommand,"If {Status} != 0\nBeep\nEnd\n");
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
- break;
- } /* case 0 */
- case 1 : /* entire word */
- {
- QuoteString(achQuote1,strTemp1); /* quote ' characters as necessary */
- if (fSearchBackward)
- {
- (void) fprintf(pfilReplaceCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
- (void) fprintf(pfilReplaceCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
- (void) fprintf(pfilReplaceCommand,"Unmark '_New_Find_' \"{Active}\"\n");
- (void) fprintf(pfilFindCommand,"Mark -y § '_New_Find_' \"{Active}\"\n");
- (void) fprintf(pfilFindCommand,"Find §¡1 \"{Active}\"\nFind Δ\\[¬{WordSet}]'%s'[¬{WordSet}]\\!1:/'%s'/Δ \"{Active}\"\nIf ({Status} != 0)\nFind '_New_Find_' \"{Active}\"\nBeep\nEnd\n",achQuote1,achQuote1);
- (void) fprintf(pfilFindCommand,"Unmark '_New_Find_' \"{Active}\"\n");
- } /* if fSearchBackward */
- else
- {
- (void) fprintf(pfilReplaceCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
- (void) fprintf(pfilReplaceCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
- (void) fprintf(pfilFindCommand,"Find Δ/[¬{WordSet}]'%s'[¬{WordSet}]/ \"{Active}\"\n",achQuote1);
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nElse\nFind /'%s'/ \"{Active}\"\nEnd\n",achQuote1);
- } /* else if fSearchBackward */
- break;
- } /* case 1 */
- case 2 : /* selection expression */
- {
- (void) fprintf(pfilReplaceCommand,"Find %s \"{Active}\"\n",strTemp1);
- (void) fprintf(pfilReplaceCommand,"If {Status} != 0\nBeep\nEnd\n");
- (void) fprintf(pfilFindCommand,"Find %s \"{Active}\"\n",strTemp1);
- (void) fprintf(pfilFindCommand,"If {Status} != 0\nBeep\nEnd\n");
- break;
- } /* case 2 */
- } /* switch sSearchType */
- } /* else if fReplace */
- (void) fclose(pfilFindCommand);
- } /* if pfilFindCommand */
- (void) fclose(pfilReplaceCommand);
- } /* if pfilReplaceCommand */
- } /* if strTemp1[] */
- if (fBeep)
- {
- SysBeep(3); /* signal error */
- } /* if fBeep */
- } /* if fGoOn */
- LDispose(hlistFind);
- LDispose(hlistReplace);
- DisposDialog(pdlogReplace); /* flush the dialog out of memory */
- } /* ReplaceDialog() */
-
- void main(void)
- {
- (void) signal(SIGINT,SIG_IGN); /* disable command-. because aborting with the dialog up is a BAD THING */
- InitGraf(&qd.thePort); /* initialize QuickDraw */
- SetFScaleDisable(true);
- FlushEvents(everyEvent,0); /* start with no pending events to handle */
- InitCursor(); /* start with a visible arrow cursor */
- (void) TEFromScrap(); /* Get any MPW text scrap */
- ReplaceDialog(); /* open the modal dialog */
- FlushEvents(everyEvent,0); /* exit with no pending events to handle */
- exit(0);
- } /* main() */
-
- /* end of NewReplaceTool.c */